<?php

class Departure extends CI_Model {

    function index() {
        $q = $this->db->get('jadwal');
        return $q->result();
    }

    function tanggal($id_maskapai, $from, $to) {
//        $array = array('id_maskapai' => $id_maskapai, 'from' => $from, 'to' => $to, 'date' => $date);
        $array = array('id_maskapai' => $id_maskapai, 'from' => $from, 'to' => $to);
        $this->db->where($array);
        $q = $this->db->get('jadwal');
        return $q;
    }

    function flight($from, $to) {
        $array = array('from' => $from, 'to' => $to);
        $this->db->where($array);
        $this->db->order_by('price', 'ASC');
        $q = $this->db->get('jadwal');
        return $q->result();
    }

    function edit($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('jadwal');
        return $q->result();
    }

    function create($id_maskapai, $flight, $from, $to, $depart, $arrives) {
        $data = array(
            'id_maskapai' => $id_maskapai,
//            'price' => $price,
            'flight' => $flight,
            'from' => $from,
            'to' => $to,
            'depart' => $depart,
            'arrives' => $arrives,
//            'class' => $class,
//            'adult' => $adult,
//            'infant' => $infant,
//            'date' => $date,
        );
        $this->db->insert('jadwal', $data);
        return true;
    }

    function update($id, $id_maskapai, $price, $flight, $from, $to, $depart, $arrives, $class, $adult, $infant, $date) {
        $data = array(
            'id_maskapai' => $id_maskapai,
            'price' => $price,
            'flight' => $flight,
            'from' => $from,
            'to' => $to,
            'depart' => $depart,
            'arrives' => $arrives,
            'class' => $class,
            'adult' => $adult,
            'infant' => $infant,
            'date' => $date,
        );

        $this->db->where('id', $id);
        $this->db->update('jadwal', $data);
//        redirect('jadwal/tanggal/' . $id_maskapai . '-' . $from . '-' . $to . '-' . $date);
        return true;
    }

    function delete($id, $id_maskapai, $from, $to) {
        $this->db->where('id', $id);
        $this->db->delete('jadwal');
        redirect('jadwal/tanggal/' . $id_maskapai . '-' . $from . '-' . $to);
    }

    function get_jadwal($id) {
        $this->db->where('id', $id);
        $q = $this->db->get('jadwal');
        foreach ($q->result() as $rows):
            $data = $rows->name;
        endforeach;

        return $data;
    }

    function update_qtty($id_jadwal, $qty) {
        $adult = $this->get_adult($id_jadwal, $qty);

        $data = array('adult' => $adult);
        $this->db->where('id', $id_jadwal);
        $this->db->update('jadwal', $data);
        return true;
    }

    function get_adult($id, $qty) {
        $this->db->where('id', $id);
        $q = $this->db->get('jadwal');
        foreach ($q->result() as $row) {
            $adult = $row->adult;
        }
        $data = $adult - $qty;
        return $data;
    }

}

?>
